package vn.cent.game.cardtype.client;



import vn.cent.game.cardtype.beans.GameBean;
import vn.cent.game.cardtype.main.CardTypeGameExtension;
import vn.cent.game.cardtype.main.RoomController;
import vn.cent.game.cardtype.utils.GameKind;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

/*
 * Quick game request handler. We have 2 case:
 * 1. User find a available room and access directly to that room.
 * Note: !Add function to make user become spector first before change him to player
 * 2. User can't find any available room 
 * ---> Make new room for him and make him host.
 */
public class RequestQuickGame extends BaseClientRequestHandler {
	
	CardTypeGameExtension extension;
	GameKind gameKind;
	User sender;
	RoomController roomController;
	
	@Override
	public void handleClientRequest(User user, ISFSObject data) {
		// TODO Auto-generated method stub
		trace("[RequestQuickGame] Handle client Request 0");
		byte tempGameKind = data.getByte("GameKind");
		sender = user;
		trace("[RequestQuickGame] Handle client Request 00");
		SetupVariables();
		
		/*
		 * Index converter
		 * 1 = BC
		 * 2 = CA
		 * Add your game index and handle function
		 */
		trace("[RequestQuickGame] Handle client Request 1");
		GameBean avaiableRoom = null;
		
		switch (tempGameKind) {
		case 1:
			gameKind = GameKind.BC;
			avaiableRoom = roomController.FindPublicBCRoom();
			trace("[RequestQuickGame] Handle client Request 2");
			break;
		case 2:
			gameKind = GameKind.CA;
			
			break;
		default:
			break;
		}
		
		trace("[RequestQuickGame] Handle client Request 3");
		if (avaiableRoom != null)
		{
			trace("[RequestQuickGame] Start join room!!!!!!!");
			JoinRoom(avaiableRoom);
		}
		else
		{
			trace("[RequestQuickGame] Start create room!!!!!!!");
			CreateRoomBaseGameKind();
		}
		
	}
	
	private void SetupVariables()
	{
		extension = (CardTypeGameExtension) getParentExtension();
		roomController = new RoomController(this.getApi(), extension, sender);
	}

	private void JoinRoom(GameBean room)
	{
		roomController.JoinPublicRoom(room);
	}
	
	private void CreateRoomBaseGameKind()
	{
		roomController.CreatePublicBCRoom();
	}
	
	
}